// pages/comment/comment.js
import { Home } from '../home/home-model.js';
var home = new Home(); //实例化 首页 对象
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info_id: '',
    s1:'../../images/comment/p1.png',
    s2:'../../images/comment/p.png',
    star:0,
    content:'',
    to_id:0,
    to_comment:0,
    cameraSrc:'../../images/comment/70.png',
    isAnonymous:false,
    btnSrc:'../../images/comment/w5.png',
    imgList:[]
  },
  setStar:function(e){
    var idx = parseInt(e.target.dataset.idx);
    idx=idx==this.data.star-1?idx:idx+1;
    this.setData({
      star:idx
    });
  },
  radioChange:function(e){
    this.setData({
      isAnonymous: !this.data.isAnonymous
    });
  },
  input: function (e) {
    var o = {};
    o[e.target.id] = e.detail.value;
    this.setData(o);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    this.setData({
      info_id: id
    });

  },

  submit:function(){
    var session_key = wx.getStorageSync('session_key');
    if (session_key == '') {
      wx.showToast({
        title: '没有登录',
        icon: 'none',
        duration: 2000
      })
      return false;
    }

    var data = {
      "session_key": session_key,
      'info_id': this.data.info_id,
      "star": this.data.star,
      'content': this.data.content,
      'to_id': this.data.to_id,
      'to_comment': this.data.to_comment,
      "isAnonymous": this.data.isAnonymous,
      "image": this.data.imgList
    }

    if (data.content == '') {
      wx.showToast({
        title: '信息填写不完整',
        icon: 'none',
        duration: 2000
      })
      return false;
    }

    home.submitComment(data, (res) => {
      if(res.status==1){
        wx.showToast({
          title: '发布成功',
          icon: 'success',
          duration: 2000
        })
        this.setData({
          star: 0,
          content:'',
          isAnonymous:false
        });
      }else{
        wx.showToast({
          title: res.info,
          icon: 'none',
          duration: 2000
        });
      }
    });


  },

  upimg: function () {
     var that = this;
      wx.chooseImage({
        sizeType: ['original', 'compressed'],
        success: function (res) {

          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths;
          home.upload(tempFilePaths[0],(url) => {
            that.setData({
              imgList: url,
              cameraSrc: url
            });

          });


        }
      })
    
  },    


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})